package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"os"
)

func main() {
	//将 数据 转换 json
	type Road struct {
		Name   string
		Number int
	}
	//将json 转换 数据
	type Server struct {
		ServerName string
		ServerIP   string
	}
	type Serverlice struct {
		Servers []Server
	}

	roads := []Road{

		{"Diamon Fork", 29},
		{"Sheep Creek", 51},
	}
	b, err := json.Marshal(roads)
	if err != nil {
		log.Fatalln(err)
	}
	var out bytes.Buffer
	err = json.Indent(&out, b, "", "\t")

	if err != nil {
		log.Fatalln(err)
	}
	out.WriteTo(os.Stdout)

	var s Serverlice
	str := `{"servers":[{"serverName":"shanghai_vpn","serverIP":"127.0.0.1"},{"serverName":"beijing_vpn","serverIP":"127.0.0.2"}]}`
	//	str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1"},
	//            {"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}]}`
	json.Unmarshal([]byte(str), &s)
	fmt.Println(s)
	fmt.Println(s.Servers[0].ServerIP)

}
